package com.ruoyi.system.mapper;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.MapPro;
import com.ruoyi.system.domain.Supplier;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 供货商Mapper接口
 * 
 * @author ruoyi
 * @date 2025-07-28
 */
public interface SupplierMapper 
{


    /**
     * 重新计算供应商评分
     * @param id
     */
    @Update("update supplier s set \n" +
            "s.star_service = (select AVG(o.star_service) from order_list o where o.supply_id = s.id and o.star_service > 0) ,\n" +
            "s.star_quality = (select AVG(o.star_quality) from order_list o where o.supply_id = s.id and o.star_quality > 0) ,\n" +
            "s.star_timely = (select AVG(o.star_timely) from order_list o where o.supply_id = s.id and o.star_timely > 0)  where s.id = #{id}")
    public void recomputeStar(@Param("id") long id);

    /**
     * 获取供应商统计数据
     * @param id
     * @return
     */
    @Select("select \n" +
            "(select count(1) from supply_user u where u.supply_id = #{id})userc,\n" +
            "(select count(1) from company_supply where supply_id= #{id})companyc,\n" +
            "(select count(1) from order_list o where o.supply_id = #{id} and o.day like concat(#{month},'%'))om,\n" +
            "(select count(1) from order_list o where o.supply_id = #{id} ) oa ,\n" +
            "(select sum(o.price) from order_list o where o.supply_id = #{id} and o.day like concat(#{month},'%')) pm,\n"+
            "(select sum(o.price) from order_list o where o.supply_id = #{id} ) pa \n"+
            "from dual ")
    public Map<String,Object> getCountData(@Param("id") long id,@Param("month") String month);

    /**
     * 查询最近7天采购单数据
     * @param id
     * @param day
     * @return
     */
    @Select("select d.date,IFNULL(o.num,0)num from dim_date d\n" +
            "left join \n" +
            "(select count(1)num,day from order_list o where o.supply_id = #{id} group by o.day)o on d.date = o.day where d.date >#{day}  order by d.date asc limit 7  ")
    public List<Map<String,Object>> getWeeklyData(@Param("id") long id, @Param("day") String day);

    /**
     * 执行绑定
     * @param supplierId
     * @param companyId
     * @return
     */
    @Update("update company_supply cs set cs.`status` = '1' where cs.company_id = #{companyId} and cs.supply_id = #{supplierId} ")
    public int bindSupplier(@Param("supplierId") long supplierId, @Param("companyId") long companyId);
    /**
     * 查询供货商
     * 
     * @param id 供货商主键
     * @return 供货商
     */
    public Supplier selectSupplierById(Long id);

    /**
     * 查询供货商列表
     * 
     * @param supplier 供货商
     * @return 供货商集合
     */
    public List<Supplier> selectSupplierList(Supplier supplier);

    /**
     * 新增供货商
     * 
     * @param supplier 供货商
     * @return 结果
     */
    public int insertSupplier(Supplier supplier);

    /**
     * 修改供货商
     * 
     * @param supplier 供货商
     * @return 结果
     */
    public int updateSupplier(Supplier supplier);

    /**
     * 删除供货商
     * 
     * @param id 供货商主键
     * @return 结果
     */
    public int deleteSupplierById(Long id);

    /**
     * 批量删除供货商
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteSupplierByIds(Long[] ids);

    /**
     * 根据信息查询公司供货商绑定列表
     * @return
     */
    public List<MapPro> getCSList(@Param("companyId") long companyId, @Param("supplierId") long supplierId, @Param("sids") String sids);
}
